programming4us
           
 
 
SQL Server

SQL Server 2008 : Indexing for Performance - Putting It All Together (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/30/2010 3:21:39 PM
This section demonstrates the terms and strategies discussed throughout this article. For each example, we show you the queries and their execution plans. The goal of this section is not to show you how to read execution plans, but to show you the performance difference displayed within execution plans for the various indexing options. There are entire books dedicated to nothing but indexing, so we won't show you every indexing method example. However, this glimpse into the performance improvements for the various types of indexing should enable you to play with the different strategies within your environment.

1. Setting Up the Scenario

For the demonstrations in this section, we use a series of sales tables within the Adventure-Works2008 database. We would like you to create copies of those tables, move data into those copies, and create some indexes on them. We are going to use three tables: the sales header table, sales details table, and the product table. The following code sample creates the tables in a separate schema and moves data into them:

USE AdventureWorks2008
GO

SELECT *
INTO AdventureWorks2008.APWRITER.Product
FROM AdventureWorks2008.Production.Product

SELECT *
INTO AdventureWorks2008.APWRITER.SalesOrderDetail
FROM AdventureWorks2008.Sales.SalesOrderDetail

SELECT *
INTO AdventureWorks2008.APWRITER.SalesOrderHeader
FROM AdventureWorks2008.Sales.SalesOrderHeader

If you do not have the APWRITER schema created, then use a schema that exists on your system or create a separate schema just for the tables. Before we begin, let's make sure you understand the relationship between the tables. The SalesOrderHeader table contains the information about when a sale was created. The SalesOrderDetail table contains a list of all of the details related to the sale. The Product table has the products that exist within each sale detail. In other words, SalesOrderHeader tables have order details and the order details have products. Enable the Include Actual Execution Plan option before you execute the following queries.

2. Table Scans

The first thing you notice if you query the three newly created tables is that the query optimizer does a table scan. As you can see, to retrieve all the data from a table, the table scan is not necessarily a bad option; however, retrieving specific records from within a heap table may prove costly. Enable execution plans in your query window, and execute the following queries. You'll see their execution plans displayed in Figure 1.

USE AdventureWorks2008
GO

SELECT *
FROM APWRITER.SalesOrderHeader

SELECT *
FROM APWRITER.SalesOrderDetail

SELECT *
FROM APWRITER.Product

Figure 1. The execution plan of querying the three tables

Now instead of just retrieving all the data from the tables, you may want to retrieve specific results from each one of the tables. The following code retrieves specific information from joining the newly created tables. Figure 2 shows the resulting execution plan.

SELECT soh.SalesOrderId, soh.OrderDate, soh.ShipDate,p.Name, sod.OrderQty,
sod.UnitPrice, sod.LineTotal
FROM apWriter.SalesOrderHeader soh JOIN apWriter.SalesOrderDetail sod
ON soh.SalesOrderId = sod.SalesOrderId
JOIN apWriter.Product p on sod.ProductId = p.ProductId
WHERE soh.SalesOrderId = 60941

Figure 2. Joining multiple tables without any indexes

The execution plan and the response time suggest that joining multiple tables together via table scan is not the most efficient way to retrieve data. So the first rule that we would like to demonstrate is the benefit of creating clustered indexes on tables.


Other -----------------
- SQL Server Integration Services : Logged and Nonlogged Operations
- SQL Server Integration Services : Using bcp (part 5)
- SQL Server Integration Services : Using bcp (part 4)
- SQL Server Integration Services : Using bcp (part 3)
- SQL Server Integration Services : Using bcp (part 2) - Fundamentals of Exporting and Importing Data
- SQL Server Integration Services : Using bcp (part 1)
- SQL Server Integration Services : Connection Projects in Visual Studio
- SQL Server Integration Services : The Package Execution Utility (part 3) - The dtutil Utility
- SQL Server Integration Services : The Package Execution Utility (part 2) - Running Packages
- SQL Server Integration Services : The Package Execution Utility (part 1)
- SQL Server Integration Services : The SSIS Designer
- SQL Server Integration Services : Running the SSIS Wizard
- SQL Server Integration Services : A Data Transformation Requirement
- SQL Server 2008 : SSIS Tools and Utilities
- SQL Server 2008 : SSIS Architecture and Concepts
- SQL Server 2008 : SQL Server Integration Services - SSIS Basics
- Defensive Error Handling : Using Transactions and XACT_ABORT to Handle Errors
- Managing Security Within the Database Engine : Securables
- Managing Security Within the Database Engine : Database Security
- Managing Security Within the Database Engine : Creating SQL Server Principals
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us